#include "src/gui/qt/loggerwidget.h"
#include <boost/algorithm/string.hpp>

void LoggerWidget::initText() {
  text = QString("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                 "<html>\n"
                 "  <head>\n"
                 "    <title>\n"
                 "      Log\n"
                 "    </title>\n"
                 "    <style type=\"text/css\">\n"
                 "      .row0 { background-color: #DDDDDD; }\n"
                 "      .row1 { background-color: #CCCCCC; }\n"
                 "      .error { color : #FF0000; }\n"
                 "      .warning {}\n"
                 "      .normal {}\n"
                 "      th { font-weight: bold;\n"
                 "           font-family: Arial; }\n"
                 "    </style>\n"
                 "  </head>\n"
                 "  <body>\n"
                 "    <table width=\"100%\">\n"
                 "<!-- NEW LOG ENTRY HERE -->\n"
                 "    </table>\n"
                 "  </body>\n"
                 "</html>\n");
}

LoggerWidget::LoggerWidget(QWidget *parent) : QTextBrowser(parent), rowType(true), priority(LoggerRecord::Unknown)
{
  empty = true;
  initText();
  setContextMenuPolicy(Qt::NoContextMenu);
}

void LoggerWidget::addRecord(const LoggerRecord& record)
{
  if (record.priority() < priority ) return;
  empty = false;
  std::string rec("      <tr class=\"ROW_TYPE\">\n"
                  "        <td class=\"RECORD_TYPE\" width=\"16\"><img src=\"IMAGE\"/></td>\n"
                  "        <td class=\"RECORD_TYPE\"  width=\"70\">TIME</td>\n"
                  "        <td class=\"RECORD_TYPE\">TEXT</td>\n"
                  "      </tr>\n"
                  "<!-- NEW LOG ENTRY HERE -->");
  boost::algorithm::replace_first(rec, "ROW_TYPE", rowType ? "row1" : "row2");
  rowType = !rowType;
  switch(record.type())
  {
  case LoggerRecord::Normal :
    boost::algorithm::replace_first(rec, "RECORD_TYPE", "normal");
    boost::algorithm::replace_first(rec, "IMAGE", ":/log/dialog-information.png");
    break;
  case LoggerRecord::Warning :
    boost::algorithm::replace_first(rec, "RECORD_TYPE", "warning");
    boost::algorithm::replace_first(rec, "IMAGE", ":/log/dialog-warning.png");
    break;
  case LoggerRecord::Error :
    boost::algorithm::replace_first(rec, "RECORD_TYPE", "error");
    boost::algorithm::replace_first(rec, "IMAGE", ":/log/dialog-error.png");
    break;
  }

  boost::algorithm::replace_first(rec, "TIME", record.time().toString("d.MM.yyyy - h:mm:ss:zzz"));
  boost::algorithm::replace_first(rec, "TEXT", record.text());
  text.replace("<!-- NEW LOG ENTRY HERE -->", QString::fromStdString(rec));
  setHtml(text);
  moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
}

void LoggerWidget::changePriority(const LoggerRecord::RecordPriority prior, const std::vector<LoggerRecord>& records)
{
  if (priority == prior) return;
  priority = prior;
  initText();
  for (std::vector<LoggerRecord>::const_iterator i = records.begin(), e = records.end(); i!=e; ++i) addRecord(*i);
}


